package com.kye.express.business.personal_my_bill.Bill.list;

import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.ErrorView;
import com.kye.express.contract.base.view.IView;
import com.kye.express.contract.base.view.LoadingView;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.UserSession;
import com.kye.model.base.RetrofitManager;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.RequestUser;
import com.kye.model.bean.bill.BillListBean;
import com.kye.model.bean.bill.FinanceInfo;
import com.kye.model.bean.bill.MonthlyBalanceBillBean;
import com.kye.model.bean.bill.request.RequestGetBillList;
import com.kye.model.bean.user.User;
import com.kye.model.service.BillService;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.SingleSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

public interface BillListContract {
    interface BillListView extends IView, LoadingView, ErrorView {

        void onNavigators(List<String> keyList, Map<String, Integer> navigators);

        void onLoadedData(BillListBean billListBean);

        void onActionLoading();

        void onCallToFinance(String financePhone);

        void onActionError(String msg);

        void onActionError();

        void onActionLoaded();
    }

    class BillListPresenter extends BasePresenter<BillListView> {

        public void load(MonthlyBalanceBillBean monthlyBalanceBillBean) {
            UserSession.getInstance()
                    .getUserForRx()
                    .firstOrError()
                    .flatMap(new Function<User, SingleSource<OpenApiResult<BillListBean>>>() {
                        @Override
                        public SingleSource<OpenApiResult<BillListBean>> apply(User user) throws Exception {
                            return RetrofitManager.getInstance()
                                    .createOpenApiService(BillService.class)
                                    .getBillList(new RequestGetBillList(user.getPhone(), monthlyBalanceBillBean.getBillDate(), monthlyBalanceBillBean.getBillType()));
                        }
                    })
                    .doOnSuccess(new Consumer<OpenApiResult<BillListBean>>() {
                        @Override
                        public void accept(OpenApiResult<BillListBean> billListBeanOpenApiResult) throws Exception {
                            Map<String, Integer> navigators = new LinkedHashMap<>();
                            List<String> keyList = new ArrayList<>();
                            List<BillListBean.BillBean> accountBillDtl = billListBeanOpenApiResult.getData().getAccountBillDtl();

                            final Calendar calendar = Calendar.getInstance();
                            for (int i = 0; i < accountBillDtl.size(); i++) {
                                BillListBean.BillBean billBean = accountBillDtl.get(i);
                                calendar.setTime(billBean.getSendTime());
                                final String key = calendar.get(Calendar.DAY_OF_MONTH) + "";
                                if (navigators.get(key) == null) {
                                    navigators.put(key, i);
                                    keyList.add(key);
                                }
                            }

                            getView().onNavigators(keyList, navigators);
                        }
                    })
                    .compose(getView().bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<BillListBean>() {
                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onSuccess(OpenApiResult<BillListBean> result, BillListBean billListBean) {
                            getView().onLoadedData(billListBean);
                        }

                        @Override
                        public void onApiError(OpenApiResult<BillListBean> result, int code, String msg) {
                            getView().onError(msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onError();
                        }

                        @Override
                        public void onLoaded() {
                            getView().onLoaded();
                        }
                    });
        }

        public void callToFinance() {
            UserSession.getInstance()
                    .getUserForRx()
                    .firstOrError()
                    .flatMap(new Function<User, SingleSource<OpenApiResult<FinanceInfo>>>() {
                        @Override
                        public SingleSource<OpenApiResult<FinanceInfo>> apply(User user) throws Exception {
                            return RetrofitManager.getInstance()
                                    .createOpenApiService(BillService.class)
                                    .getFinanceInfo(new RequestUser(user.getPhone()));
                        }
                    })
                    .compose(getView().bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<FinanceInfo>() {
                        @Override
                        public void onLoading(Disposable d) {
                            getView().onActionLoading();
                        }

                        @Override
                        public void onSuccess(OpenApiResult<FinanceInfo> result, FinanceInfo financeInfo) {
                            getView().onCallToFinance(financeInfo.getFinancePhone());
                        }

                        @Override
                        public void onApiError(OpenApiResult<FinanceInfo> result, int code, String msg) {
                            getView().onActionError(msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onActionError();
                        }

                        @Override
                        public void onLoaded() {
                            getView().onActionLoaded();
                        }
                    });
        }
    }
}
